//
//  SearchAccessoryController.m
//  Home
//
//  Created by 新宝 on 2017/8/8.
//  Copyright © 2017年 新宝. All rights reserved.
//

#import "SearchAccessoryController.h"

@interface SearchAccessoryController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)NSMutableArray *acceArry;
@property(nonatomic,strong)UITableView *accessoryTableView;
@property(nonatomic,strong)UIButton *button;
@end

@implementation SearchAccessoryController
-(NSMutableArray *)acceArry
{
    if(!_acceArry)
    {
        _acceArry=[[NSMutableArray alloc]init];
    }
    return _acceArry;
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.hidden=NO;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:YES];
    //self.navigationController.navigationBar.hidden = YES;
    [self.browser stopSearchingForNewAccessories];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.button=[[UIButton alloc]initWithFrame:CGRectMake(0, 100, 80, 30)];
    [self.button setTitle:@"搜索设备" forState:UIControlStateNormal];
    [self.button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [self.button addTarget:self action:@selector(searchAccessoryButton) forControlEvents: UIControlEventTouchDown];
    UIBarButtonItem *rightItem=[[UIBarButtonItem alloc]initWithCustomView:self.button];
    self.navigationItem.rightBarButtonItem=rightItem;
    
    
    self.navigationItem.title=[NSString stringWithFormat:@"家:%@中的房间:%@",self.currentHome.name,self.currentRoom.name];
    self.accessoryTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
    self.accessoryTableView.delegate=self;
    self.accessoryTableView.dataSource=self;
    [self.view addSubview:self.accessoryTableView];
    
    self.browser=[[HMAccessoryBrowser alloc]init];
    self.browser.delegate=self;
    
}

-(void)searchAccessoryButton
{
    NSLog(@"开始搜索设备-------");
    [self.browser startSearchingForNewAccessories];
}


#pragma mark accessoryBroser代理方法
//每次发现一个智能硬件这个方法都会调用一次
-(void)accessoryBrowser:(HMAccessoryBrowser *)browser didFindNewAccessory:(HMAccessory *)accessory
{
    NSLog(@"发现一个新硬件-----%@",accessory);
    [self.acceArry addObject:accessory];
   [self.accessoryTableView reloadData];
    
}
-(void)accessoryBrowser:(HMAccessoryBrowser *)browser didRemoveNewAccessory:(HMAccessory *)accessory
{
    NSLog(@"删除一个硬件");
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.acceArry.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[[UITableViewCell alloc]init];
    HMAccessory *accessory=self.acceArry[indexPath.row];
    cell.textLabel.text=accessory.name;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HMAccessory *accessory=self.acceArry[indexPath.row];
    __block HMHome *home=self.currentHome;
    __block HMRoom *room=self.currentRoom;
    [self.currentHome addAccessory:accessory completionHandler:^(NSError * _Nullable error) {
        if(!error)
        {
            if(accessory.room!=room)
            {
                [home assignAccessory:accessory toRoom:room completionHandler:^(NSError * _Nullable error) {
                    if(!error)
                    {
                        NSLog(@"已经将配件放进了%@",room.name);
                    }
                    else
                    {
                        NSLog(@"指定房间失败");
                    }
                    
                }];
            }
            
        }
        else
        {
            NSLog(@"添加配件到Home失败-----%@",error);
        }
    }];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
